package com.szq.netty.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @ClassName: NettyServer
 * @Description:
 * @Author: szq
 * @Date: 2021-11-09 17:15:58
 */
public class NettyClient {

    public static void main(String[] args) {
        new NettyClient().connect("192.168.30.32", 7397);
    }

    private void connect(String inetHost, int port) {
        //配置服务端NIO线程组
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(workGroup)
                .channel(NioSocketChannel.class)  //非阻塞模式
                .option(ChannelOption.AUTO_READ, true)
                .handler(new MyChannelInitializer());

            ChannelFuture f  = b.connect(inetHost, port).sync();
            System.out.println("netty-demo-107 client start done");
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            workGroup.shutdownGracefully();
        }

    }
}
